package com.cskaoyan.market.service.admin.impl;

import com.cskaoyan.market.db.domain.MarketIssue;
import com.cskaoyan.market.db.domain.MarketIssueExample;
import com.cskaoyan.market.db.mapper.MarketIssueMapper;
import com.cskaoyan.market.service.admin.MarketIssueService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class MarketIssueServiceImpl implements MarketIssueService {
    @Autowired
    MarketIssueMapper issueMapper;

    @Override
    public List<MarketIssue> list(Integer page, Integer limit, String sort, String order, String id, String question, String answer, String addTime, String updateTime) {


        // new example
        MarketIssueExample issueExample = new MarketIssueExample();
        // or 方法
        MarketIssueExample.Criteria criteria = issueExample.or();
        // page limit 分页使用
        // sort order 排序使用
        // id question answer addTime updateTime 条件
        // 没有传的情况下不会走到下面里面
        if (!StringUtils.isEmpty(id)){
            criteria.andIdEqualTo(Integer.parseInt(id));
        }
        if (!StringUtils.isEmpty(question)){
            criteria.andQuestionLike("%"+ question + "%");
        }
        if (!StringUtils.isEmpty(answer)){
            criteria.andAnswerEqualTo(answer);
        }
        if (!StringUtils.isEmpty(addTime)){
            criteria.andAddTimeEqualTo(LocalDateTime.parse(addTime));
        }
        if(!StringUtils.isEmpty(updateTime)){
            criteria.andUpdateTimeEqualTo(LocalDateTime.parse(updateTime));
        }
        // 判断deleted
        criteria.andDeletedEqualTo(false);
        // 排序
        issueExample.setOrderByClause(sort + " " + order);
        // 分页
        PageHelper.startPage(page,limit);
        List<MarketIssue> marketIssueList = issueMapper.selectByExample(issueExample);


        return marketIssueList;
    }

    @Override
    public MarketIssue create(MarketIssue marketIssue) {

        marketIssue.setUpdateTime(LocalDateTime.now());
        marketIssue.setAddTime(LocalDateTime.now());
        marketIssue.setDeleted(false);

        issueMapper.insert(marketIssue);

        return marketIssue;
    }

    // 修改
    @Override
    public int update(Integer id, String question, String answer, String addTime, String updateTime) {

        // new example
        MarketIssueExample issueExample = new MarketIssueExample();
        // new marketIssue对象
        MarketIssue marketIssue = new MarketIssue();
        // or 方法
        MarketIssueExample.Criteria criteria = issueExample.or();
        // 添加属性
        marketIssue.setId(id);
        marketIssue.setQuestion(question);
        marketIssue.setAnswer(answer);

        criteria.andIdEqualTo(id);
        int row = issueMapper.updateByExampleSelective(marketIssue,issueExample);

        return row;
    }

    // 删除
    @Override
    public int delete(Integer id) {

        // new example
        MarketIssueExample issueExample = new MarketIssueExample();
        // new marketIssue对象
        MarketIssue marketIssue = new MarketIssue();
        // or 方法
        MarketIssueExample.Criteria criteria = issueExample.or();

        marketIssue.setDeleted(true);
        issueExample.or().andIdEqualTo(id);
        int row = issueMapper.updateByExampleSelective(marketIssue,issueExample);

        return row;
    }
}